package com.zy.settings.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.google.gson.Gson
import com.zy.activity.BaseFragment
import com.zy.net.NetServiceImpl
import com.zy.settings.R
import com.zy.settings.SettingsConstant
import com.zy.settings.SettingsMainActivity
import com.zy.storage.DataBaseUtils
import com.zy.storage.doc.LocalDocInfo
import com.zy.utils.ThreadUtils
import com.zy.zlog.ZLog
import okhttp3.*
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException


private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
private const val TAG = "DataBackupFragment"

class DataBackupFragment : BaseFragment() {
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_data_backup, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        view.findViewById<View>(R.id.backup_diy_node).setOnClickListener {
            //开始备份
            Toast.makeText(this.requireContext(), "开始备份", Toast.LENGTH_SHORT).show()
            ThreadUtils.ioPostDelayed(1000) {
                //获取数据
                val dao = DataBaseUtils.getDocInfoDao()
                val allList = dao.getAll()
                ZLog.i("DataBaseUtils", "allList:" + allList.size)
                val allListStr = Gson().toJson(LocalDocInfo(allList.size, allList))
                ZLog.i("DataBaseUtils", "allListStr:$allListStr")
                update(allListStr)
            }
        }

        view.findViewById<View>(R.id.backup_diy_node_history).setOnClickListener {
            activity?.let { ac ->
                ac.startActivity(Intent(ac, SettingsMainActivity::class.java).apply {
                    putExtra(SettingsConstant.SETTINGS_CODE, SettingsConstant.DATA_BACKUP_HISTORY)
                })
            }
        }

        view.findViewById<View>(R.id.backup_other_btn).setOnClickListener {

        }
    }

    private fun update(allListStr: String?) {
        allListStr ?: return
        DataBaseUtils.getDocInfoDao()
        val body: RequestBody = allListStr.toRequestBody(NetServiceImpl.JSON)
        val request: Request = Request.Builder().url(NetServiceImpl.UPDATE_URL).post(body).build()
        NetServiceImpl.INSTANCE.request(request, object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                ZLog.d(TAG, "onFailure:${e}")
                showSuccess("开始失败$e")
            }

            override fun onResponse(call: Call, response: Response) {
                ZLog.d(TAG, "onResponse:${response.body?.string()}")
                showSuccess(response.message)
            }
        })
    }

    fun showSuccess(msg: String) {
        ThreadUtils.mainRun {
            Toast.makeText(activity, "备份成功 $msg", Toast.LENGTH_LONG).show()
        }
    }

    companion object {

        @JvmStatic
        fun newInstance() = DataBackupFragment().apply {
            arguments = Bundle().apply {
                putString(ARG_PARAM1, "param1")
                putString(ARG_PARAM2, "param2")
            }
        }
    }
}